<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { Toastloading, ToastWarning } from '@/utils/public';
  import { giftClassifyList } from '@/pinia/getter';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import Pagination from "@/components/Pagination/index.vue";
  import serachUserid from '@/components/serachUserid/index.vue';
  import xmDatePicker from "@/components/xmDatePicker/index.vue";
  import table2excel from 'js-table2excel';
  import giftBagList from './giftBagList.vue'
  const giftBagListRef = ref();
  const action = reactive({
    total:0,
    list:[] as any[],
    bagGiftPlatformType:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis'))
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    platform_type:'',
    user_id:''
  })
  const handleSerach = () => {
    listQuery.page = 1;
    getList();
  };
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中...')
    const { playTime:time_st, endTime:time_et } = action.timeObj
    const res = await propApi.giftBagLogList({
      ...listQuery,
      page:1,
      limit:action.total || 20,
      time_st,
      time_et
    })
    loading.close()
    const { data=[]} = res.data
    loading.close()
    if(data.length == 0) {
      ToastWarning('暂无可导出数据')
      return
    }
    const list = data.map((item:any)=>{
      return {
        ...item,
        gift_type_text:giftClassifyList.find(res=>res.type == item.gift_type)?.label || item.gift_type
      }
    })
    const tHeader = [
      { title: '记录ID', key: "id"},
      { title: '用户ID', key: "user_id"},
      { title: '用户UUID', key: "uuid"},
      { title: '用户昵称', key: "nickname"},
      { title: '关联用户ID', key: "to_user_id"},
      { title: '关联用户UUID', key: "to_uuid"},
      { title: '关联用户昵称', key: "to_nickname"},
      { title: '礼物ID', key: "gift_id"},
      { title: '礼物类型', key: "gift_type_text"},
      { title: '礼物名称', key: "gift_name"},
      { title: '来源类型', key: "platform_type_name"},
      { title: '数量', key: "change"},
      { title: '剩余数量', key: "avail"},
      { title: '时间', key: "create_time"},
    ]
    table2excel(tHeader, list, '背包流水记录表')
  }
  const getbagGiftPlatformType = async()=>{
    const loading = Toastloading()
    const res = await propApi.bagGiftPlatformType()
    const { data=[] } = res
    action.bagGiftPlatformType = data
    loading.close()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:time_st, endTime:time_et } = action.timeObj
    const res = await propApi.giftBagLogList({
      ...listQuery,
      time_st,
      time_et
    })
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(async()=>{
    await getbagGiftPlatformType()
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <serachUserid v-model:user_id="listQuery.user_id" />
        </div>
        <div class="labelItem">
          <div class="label">来源类型：</div>
          <el-select v-model="listQuery.platform_type" placeholder="请选择来源类型">
            <el-option
              label="全部来源类型"
              :value="''"
            />
            <el-option
              v-for="item in action.bagGiftPlatformType"
              :label="item.name"
              :value="item.platform_type"
            />
          </el-select>
        </div>
        <xmDatePicker v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">搜索</el-button>
          <el-button type="warning" @click="handleDownload">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="id" label="记录ID" />
      <el-table-column fixed="left" label="用户">
        <el-table-column width="120" prop="user_id" label="用户ID" />
        <el-table-column width="120" prop="uuid" label="用户UUID" />
        <el-table-column min-width="180" show-overflow-tooltip prop="nickname" label="用户昵称" />
      </el-table-column>
      <el-table-column label="关联用户">
        <el-table-column width="120" prop="to_user_id" label="关联用户ID" />
        <el-table-column width="120" prop="to_uuid" label="关联用户UUID" />
        <el-table-column min-width="180" show-overflow-tooltip prop="to_nickname" label="关联用户昵称" />
      </el-table-column>
      <el-table-column label="礼物">
        <el-table-column width="80" prop="gift_id" label="礼物ID" />
        <el-table-column width="120" prop="gift_type" label="礼物类型" >
          <template #default="scoped">
            {{ giftClassifyList.find(res=>res.type == scoped.row.gift_type)?.label || scoped.row.gift_type }}
          </template>
        </el-table-column>
        <el-table-column width="180" prop="gift_name" label="礼物名称" />
      </el-table-column>
      <el-table-column width="180" prop="platform_type_name" label="来源类型" />
      <el-table-column sortable min-width="120" prop="change" label="数量" />
      <el-table-column sortable min-width="120" prop="avail" label="剩余数量" />
      <el-table-column width="180" prop="create_time" label="时间" />
      <el-table-column fixed="right" width="180" label="操作">
        <template #default="scoped">
          <el-button type="primary" @click="giftBagListRef.openDialog(scoped.row)">当前背包礼物</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <giftBagList ref="giftBagListRef" />
  </div>
</template>